package com.ruoyi.delivery.controller;

import com.ruoyi.delivery.domain.DelAllocationOrder;
import com.ruoyi.delivery.domain.DelAllocationDetail;
import com.ruoyi.delivery.service.IDelAllocationOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 调拨单 控制层
 *
 *
 */
@RestController
@RequestMapping("/delivery/allocationOrder")
public class DelAllocationOrderController {

    @Autowired
    private IDelAllocationOrderService allocationOrderService;

    /**
     * 获取调拨单列表，支持分页和筛选
     *
     * @param allocationNo 调拨单号（模糊搜索）
     * @param origin       发货地
     * @param destination  目的地
     * @param departStartDate 调拨日期范围的起始日期
     * @param departEndDate 调拨日期范围的结束日期
     * @param arriveStartDate 预计到达日期范围的起始日期
     * @param arriveEndDate 预计到达日期范围的结束日期
     * @param state        调拨单状态
     * @param orderNo  订单号（模糊搜索）
     * @param page         页码
     * @param pageSize     每页记录数
     * @return 分页调拨单列表
     */
    @GetMapping("/list")
    public Map<String, Object> getAllocationOrderList(
            @RequestParam(required = false) String allocationNo,
            @RequestParam(required = false) String origin,
            @RequestParam(required = false) String destination,
            @RequestParam(required = false) String departStartDate,
            @RequestParam(required = false) String departEndDate,
            @RequestParam(required = false) String arriveStartDate,
            @RequestParam(required = false) String arriveEndDate,
            @RequestParam(required = false) String state,
            @RequestParam(required = false) String orderNo,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        List<DelAllocationOrder> orders = allocationOrderService.filterAllocationOrders(
                 allocationNo, origin, destination, departStartDate, departEndDate,arriveStartDate, arriveEndDate, state, orderNo, page, pageSize
        );

        // 组装分页信息
        Map<String, Object> response = new HashMap<>();
        response.put("data", orders);
        response.put("total", orders.size()); // 假设总数和查询结果一致
        response.put("page", page);
        response.put("pageSize", pageSize);
        return response;
    }

    @PostMapping("/generateTrackingNumber")
    public Map<String, String> generateTrackingNumber() {
        String trackingNumber = allocationOrderService.generateTrackingNumber();
        System.out.println(trackingNumber);
        Map<String, String> response = new HashMap<>();
        response.put("trackingNumber", trackingNumber);
        return response;
    }

    /**
     * 添加调拨单
     *
     * @param order 调拨单对象
     * @return 操作结果
     */
    @PostMapping("/add")
    public Map<String, Object> addAllocationOrder(@RequestBody DelAllocationOrder order) {
        boolean success = allocationOrderService.insertAllocationOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 删除调拨单
     *
     * @param allocationNo 调拨单号
     * @return 操作结果
     */
    @DeleteMapping("/delete")
    public Map<String, Object> deleteAllocationOrder(@RequestParam String allocationNo) {
        boolean success = allocationOrderService.deleteAllocationOrder(allocationNo);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 修改调拨单
     *
     * @param order 调拨单对象
     * @return 操作结果
     */
    @PutMapping("/update")
    public Map<String, Object> updateAllocationOrder(@RequestBody DelAllocationOrder order) {
        boolean success = allocationOrderService.updateAllocationOrder(order);
        Map<String, Object> response = new HashMap<>();
        response.put("success", success);
        return response;
    }

    /**
     * 获取运输订单明细
     *
     * @param allocationNo 调拨单号
     * @return 运输订单明细列表
     */
    @GetMapping("/details")
    public Map<String, Object> getAllocationOrderDetails(@RequestParam String allocationNo) {
        List<DelAllocationDetail> details = allocationOrderService.getDetailsByAllocationNo(allocationNo);
        Map<String, Object> response = new HashMap<>();
        response.put("data", details);
        return response;
    }
}
